package com.flame.user.controller;

import com.flame.auth.core.annotation.HasAuth;
import com.flame.core.protocol.Result;
import com.flame.user.controller.vo.RemoveOrgUserVo;
import com.flame.user.entity.OrgInfo;
import com.flame.user.service.OrgInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * flame_org_info 组织机构表
 *
 * @author 吴欣童
 * @since 2023-02-07 17:41:59
 */
@RestController
public class OrgInfoController {

    @Autowired
    private OrgInfoService orgInfoService;

    @PostMapping("/org/list")
    public Result listOrgInfo() {
        return Result.success(orgInfoService.listOrgInfo());
    }

    @HasAuth("org:add")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/org/add")
    public Result addOrgInfo(@RequestBody @Validated OrgInfo orgInfo) {
        orgInfoService.addOrgInfo(orgInfo);
        return Result.success();
    }

    @HasAuth("org:edit")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/org/update")
    public Result updateOrgInfo(@RequestBody @Validated OrgInfo orgInfo) {
        orgInfoService.updateOrgInfo(orgInfo);
        return Result.success();
    }

    @HasAuth("org:delete")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/org/delete")
    public Result deleteOrgInfo(@RequestBody @Validated OrgInfo orgInfo) {
        orgInfoService.deleteOrgInfo(orgInfo.getOrgCode());
        return Result.success();
    }

    @HasAuth("org")
    @PostMapping("/org/user/list")
    public Result listOrgUser(@RequestBody OrgInfo orgInfo) {
        return Result.success(orgInfoService.listOrgUser(orgInfo.getOrgCode()));
    }
}
